// Editor: myBigPro9/src/services/imageQuestionService.js
// Action: Create
import api from './api';

const imageQuestionService = {
  /**
   * 上传图片文件进行搜题
   * @param {File} imageFile 用户上传的图片文件
   * @param {Function} onUploadProgress 上传进度回调
   * @returns {Promise<object>} 包含问题和答案的对象
   */
  async searchByImage(imageFile, onUploadProgress) {
    const formData = new FormData();
    formData.append('image', imageFile);

    try {
      const response = await api.post('/api/image-question/search', formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        onUploadProgress,
      });
      return response.data;
    } catch (error) {
      console.error('[ImageQuestionService] 搜题失败:', error);
      const errorMessage = error.response?.data?.error || error.message || '请求服务器失败';
      throw new Error(errorMessage);
    }
  },
};

export default imageQuestionService;